MAIN := paper
LTMK := bin/latexmk
DEPS := abstract.txt

.PHONY: all clean

define latexmk =
	$(LTMK) -silent -pdf "$1" &>/dev/null \
	  || (bin/parse-latex-log.py "$1.log"; exit 1)
endef

all: $(DEPS)
	$(LTMK) -silent -pdf $(MAIN) &>/dev/null \
	  || (bin/parse-latex-log.py "$(MAIN).log"; exit 1)	

clean:
	$(LTMK) -C

SPELLTEX := $(shell ./bin/get-tex-files.sh $(MAIN).tex)
spell:
	@for i in $(SPELLTEX); do aspell -x --mode=tex -p ./aspell.words -c $$i; done
	@( head -1 aspell.words ; tail -n +2 aspell.words | sort ) > aspell.words~
	@mv aspell.words~ aspell.words

define getpdf
	pdftk $@ cat $1 output fig/$2
	pdfcrop fig/$2 fig/$2
endef

SYS := $(shell sed -n -e 's/\\newcommand{\\sys}{\\mbox{\([^}]*\).*/\1/p' macros.tex)
abstract.txt: abstract.tex
	cat $< |grep -v '\\begin' | grep -v '\\end' | \
	    sed -e '/^%/d' | \
	    sed -e 's/\\sys/$(SYS)/g' | \
	    sed -e 's/~/ /g' | \
	    sed -e 's/{//g' | \
	    sed -e 's/}//g' | \
	    sed -e 's/\\%/%/g' | \
	    fmt -w72 > $@

